<?php
class View extends Object {
	var $name = null;

	var $action = null;

	var $params = array();

	var $passedArgs = array();

	var $data = array();

	var $helpers = array('Html');

	var $viewPath = null;

	var $viewVars = array();

	var $layout = 'default';

	var $layoutPath = null;

	var $controllerPath;

	var $autoRender = true;

	var $autoLayout = true;

	var $ext = VIEW_EXT;

	var $subDir = null;

	var $validationErrors = array();

	var $loaded = array();

	var $field = null;

	var $fieldSuffix = null;

	var $modelId = null;

	var $output = false;

	var $__passedVars = array(
		'viewVars', 'action', 'autoLayout', 'autoRender', 'ext', 'base', 'webroot',
		'helpers', 'here', 'layout', 'name', 'layoutPath', 'viewPath',
		'params', 'data', 'plugin', 'passedArgs', 'cacheAction'
	);


	function __construct(&$controller) {

		if (is_object($controller)) {
			$this->action = $controller->action;
			$this->controllerPath = $controller->viewPath;
			$this->autoLayout = $controller->autoLayout;
			$this->viewVars = $controller->viewVars;
//			$this->action = $controller->action;

	/*		$count = count($this->__passedVars);
			for ($j = 0; $j < $count; $j++) {
				$var = $this->__passedVars[$j];
				$this->{$var} = $controller->{$var};
			}
	*/	}

		parent::__construct();
	}

	function element($name, $params = array(), $loadHelpers = false) {
		$file = $plugin = $key = null;

		if (isset($params['plugin'])) {
			$plugin = $params['plugin'];
		}

		if (isset($this->plugin) && !$plugin) {
			$plugin = $this->plugin;
		}

		if (isset($params['cache'])) {
			$expires = '+1 day';

			if (is_array($params['cache'])) {
				$expires = $params['cache']['time'];
				$key = Inflector::slug($params['cache']['key']);
			} elseif ($params['cache'] !== true) {
				$expires = $params['cache'];
				$key = implode('_', array_keys($params));
			}

			if ($expires) {
				$cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
				$cache = cache('views' . DS . $cacheFile, null, $expires);

				if (is_string($cache)) {
					return $cache;
				}
			}
		}
		$paths = $this->_paths($plugin);

		foreach ($paths as $path) {
			if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
				$file = $path . 'elements' . DS . $name . $this->ext;
				break;
			} elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) {
				$file = $path . 'elements' . DS . $name . '.thtml';
				break;
			}
		}

		if (is_file($file)) {
			$params = array_merge_recursive($params, $this->loaded);
			$element = $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers);
			if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
				cache('views' . DS . $cacheFile, $element, $expires);
			}
			return $element;
		}
		$file = $paths[0] . 'elements' . DS . $name . $this->ext;

		if (Configure::read() > 0) {
			return "Not Found: " . $file;
		}
	}

	function render($path){
		$output = $this->_render($path, $this->viewVars);
		return $output;
	}

	function renderLayout($content_for_layout){
		$layoutFilename = LAYOUTS.$this->layout.VIEW_EXT;

		return $this->_render($layoutFilename, array('content_for_layout'=>$content_for_layout));

		$content_for_layout = $content_for_layout;
		ob_start();

		include ($layoutFilename);

		$output = ob_get_clean();
		return $output;
	}

	function renderLayoutDummy($content_for_layout, $layout = null) {
		$layoutFileName = $this->_getLayoutFileName($layout);
		if (empty($layoutFileName)) {
			return $this->output;
		}


		$data_for_layout = array_merge($this->viewVars, array(
			'content_for_layout' => $content_for_layout,
		));

		if (empty($this->loaded) && !empty($this->helpers)) {
			$loadHelpers = true;
		} else {
			$loadHelpers = false;
			$data_for_layout = array_merge($data_for_layout, $this->loaded);
		}

		$this->_triggerHelpers('beforeLayout');

		if (substr($layoutFileName, -3) === 'ctp' || substr($layoutFileName, -5) === 'thtml') {
			$this->output = View::_render($layoutFileName, $data_for_layout, $loadHelpers, true);
		} else {
			$this->output = $this->_render($layoutFileName, $data_for_layout, $loadHelpers);
		}

		if ($this->output === false) {
			$this->output = $this->_render($layoutFileName, $data_for_layout);
			$msg = __("Error in layout %s, got: <blockquote>%s</blockquote>", true);
			trigger_error(sprintf($msg, $layoutFileName, $this->output), E_USER_ERROR);
			return false;
		}

		$this->_triggerHelpers('afterLayout');

		return $this->output;
	}

	function getVars() {
		return array_keys($this->viewVars);
	}

	function getVar($var) {
		if (!isset($this->viewVars[$var])) {
			return null;
		} else {
			return $this->viewVars[$var];
		}
	}

	function error($code, $name, $message) {
		header ("HTTP/1.1 {$code} {$name}");
		print ($this->_render(
			$this->_getLayoutFileName('error'),
			array('code' => $code, 'name' => $name, 'message' => $message)
		));
	}
	function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
		extract($___dataForView, EXTR_SKIP);
		ob_start();

		include ($___viewFn);

		$out = ob_get_clean();
		return $out;
	}
}
?>